<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class Core_Gateway_Privilege extends RedCross_Gateway_GatewayAbstract
{
    /**
     * Constructor
     * Defines some private variables
     */
    public function __construct()
    {
        $this->_class = 'Privilege';
        $this->_modelPath = 'Core_Model';
    }

    public function fetchAllByGroup($groupId)
    {
        $result = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() .' p')
            ->leftJoin('p.Resource r')
            ->leftJoin('p.Groups g')
            ->where('g.id = ?', $groupId)
            ->execute();
        return $result;
    }

    public function fetchAllResourcesWithPrivileges()
    {
        $result = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_Resource r')
            ->leftJoin('r.Privileges p')
            ->orderBy('r.name, p.name')
            ->execute();
        return $result;
    }

    public function removeGroupPrivileges($groupId, array $privilegeIds = null)
    {
        $q = Doctrine_Query::create()
            ->delete('RedCross_Gateway_Base_GroupPrivilege')
            ->addWhere('group_id = ?', $groupId);
        if ($privilegeIds !== null) {
        	$q->whereIn('privilege_id', $privilegeIds);
        }
        return $q->execute();
    }
}